package com.cp_smss.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cp_smss.dao.UserDao;
import com.cp_smss.model.PageModel;
import com.cp_smss.model.User;

@Repository
public class UserDaoImpl implements UserDao {
	
	@Autowired
	private SqlSession sqlSession;
	
	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	@Override
	public User getUserById(String userId) {
		return sqlSession.selectOne("com.cp_smss.model.UserMapper.getUserById",userId);
	}

	@Override
	public int addUser(User user) {
		return sqlSession.insert("com.cp_smss.model.UserMapper.addUser", user);
	}
	
	@Override
	public int updateUser(User user) {
		return sqlSession.update("com.cp_smss.model.UserMapper.updateUser", user);
	}

	@Override
	public int delUser(String userId) {
		return sqlSession.update("com.cp_smss.model.UserMapper.delUser", userId);
	}

	@Override
	public PageModel<User> listUsers(int pageNo,int pageSize){
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		List<User> users = sqlSession.selectList("com.cp_smss.model.UserMapper.listUser",map);
		int totalRecords = sqlSession.selectOne("com.cp_smss.model.UserMapper.getTotalRecords");
		PageModel<User> pm = null;
		if(users!=null){
			pm = new PageModel<User>();
			pm.setPageNo(pageNo);
			pm.setPageSize(pageSize);
			pm.setList(users);
			pm.setTotalRecords(totalRecords);
		}
		return pm;
	}


	@Override
	public User getUserByCondition(User user) {
		return (User)sqlSession.selectOne("com.cp_smss.model.UserMapper.getUserByCondition", user);
	}
	
}
